/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.Random;

/**
 *
 * @author Jhonatan
 */
public class distribuciones {
    
    
    public double exponencial(double inferior, double superior,double lambda){
        double d=0,u=0,w=0,y=0;
        Random ran = new Random();
        //calculo de num aleatorio
        u = ran.nextDouble();
        //truncamiento
        w= (1-Math.exp((inferior/lambda)*(-1)))+(1-Math.exp((superior/lambda)*(-1))-(1-Math.exp((inferior/lambda)*(-1))))*u;
        //calculo de valor de la distribucion
        y= -lambda*Math.log(1-w);
        return y;
    }
    
    public double weibull(double inferior, double superior,double alfa,double beta){
        double d=0,u=0,w=0,y=0;
        Random ran = new Random();
        //calculo de num aleatorio
        u = ran.nextDouble();
        //truncamiento
        w= (1-Math.exp(Math.pow((inferior/alfa),beta)*(-1)))+ ((1-Math.exp(Math.pow((superior/alfa),beta)*(-1)))-(1-Math.exp(Math.pow((inferior/alfa),beta)*(-1))));
        //calculo de valor de la distribucion
        y= (Math.pow(-Math.log(1-w),(1/beta)))*alfa;
        return y;
    }
    
    public double normal(double inferior, double superior,double media,double varianza){
        double d=0,u=0,w=0,y=0;
        Random ran = new Random();
        //calculo de num aleatorio
        u = ran.nextDouble();
        //truncamiento
        w= 1/varianza*Math.sqrt(2*Math.PI)*Math.exp(-1/2*Math.pow((inferior-media/varianza),2))+(1/varianza*Math.sqrt(2*Math.PI)*Math.exp(-1/2*Math.pow((superior-media/varianza),2))-1/varianza*Math.sqrt(2*Math.PI)*Math.exp(-1/2*Math.pow((inferior-media/varianza),2)));
        //calculo de valor de la distribucion
        y= media+(varianza*Math.sqrt(-2*(Math.log(w*(varianza*(Math.sqrt(2*Math.PI)))))));
        return y;
    }
    
    public double uniforme(double inferior, double superior,double a,double b){
        double d=0,u=0,w=0,y=0;
        Random ran = new Random();
        //calculo de num aleatorio
        u = ran.nextDouble();
        //truncamiento
        w= calcUniforme(a, b, inferior)+(calcUniforme(a, b, superior)-calcUniforme(a, b, inferior));
        //calculo de valor de la distribucion
        
        y=a+(w*(b-a));
        return y;
    }
    
    public double triangular(double inferior, double superior,double a,double b,double c){
        double d=0,u=0,w=0,y=0;
        Random ran = new Random();
        //calculo de num aleatorio
        u = ran.nextDouble();
        //truncamiento
        w= calcTriangular(a, b, c, inferior)+(calcTriangular(a, b, c, superior)-calcTriangular(a, b, c, inferior));
        //calculo de valor de la distribucion
        y=a+Math.sqrt((1-w)*(c-b)*(c-a));
        return y;
    }
    
//    public double poisson(double inferior, double superior,double lambda){
//        double d=0,u=0,w=0,y=0;
//        Random ran = new Random();
//        //calculo de num aleatorio
//        u = ran.nextDouble();
//        //truncamiento
//        w= (Math.pow(lambda,inferior)*Math.exp(-lambda))/factorial(inferior)+((Math.pow(lambda,superior)*Math.exp(-lambda))/factorial(superior)-(Math.pow(lambda,inferior)*Math.exp(-lambda))/factorial(inferior));
//        //calculo de valor de la distribucion
//        y=a+Math.sqrt((1-w)*(c-b)*(c-a));
//        return y;
//    }
    public double calcUniforme(double a,double b,double x){
        double res=0;
        if(x<=a)
            return res=0;
        if(x>=b)
            return res=1;
        if(x>a&&x<b)
            return res = (x-a)/(b-a);
        
        return res;
    }
    
     public double calcTriangular(double a,double b,double c,double x){
        double res=0;
        if(x<=a)
            return res=0;
        if(x>c)
            return res=1;
        if(x>a&&x<=b)
            return res = (Math.sqrt(x-a))/(b-a)*(c-a);
        if(x>b&&x<=c)
            return res = 1-((Math.sqrt(c-x))/(c-b)*(c-a));
        
        return res;
    }
     
    public double factorial(double x){
        double factorial =1;
        while(x!=0){
            factorial=factorial*x;
            x--;
        }
        return factorial;
        
    }
}
